import {createSlice} from '@reduxjs/toolkit';

// 定义RootState类型
export interface RootState {
  counter: {
    value: number;
  };
}

export const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    value: 0,
  },
  reducers: {
    increment: state => {
      state.value += 1;
    },
    decrement: state => {
      state.value -= 1;
    },
    incrementByAmount: (state, action) => {
      state.value += action.payload;
    },
  },
});

// 导出选择器
export const selectCount = (state: RootState) => state.counter.value;

export const {increment, decrement, incrementByAmount} = counterSlice.actions;
export default counterSlice.reducer;